package com.example.administrator.textproject;

/**
 * Created by Administrator on 2017/3/14 0014.
 */

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioButton;
import android.widget.RadioGroup;

public class TwoFragment extends Fragment implements RadioGroup.OnCheckedChangeListener {
    private RadioGroup radioGroup;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
       View rootView= inflater.inflate(R.layout.fragment_two,container,false);
        viewInint(rootView);

        return rootView;
    }

    private void viewInint(View rootView) {
      radioGroup= (RadioGroup) rootView.findViewById(R.id.nav_tab_container);
       RadioButton radioButton= (RadioButton) radioGroup.getChildAt(0);
        radioButton.setChecked(true);
//        ((RadioButton)radioGroup.findViewById(R.id.conversation_btn)).setChecked(true);
        radioGroup.setOnCheckedChangeListener(this);

    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        switch (checkedId){
            case R.id.conversation_btn:
                OneFragment_1 oneFragment1=new OneFragment_1();
                addFragment(oneFragment1,"OneFragment_1");
                break;
            case R.id.friend_btn:
                TwoFragment_2 twoFragment2=new TwoFragment_2();
                addFragment(twoFragment2,"TwoFragment_2");
                break;
            case R.id.dynamic_btn:
                ThreeFragment_3 threeFragment3=new ThreeFragment_3();
                addFragment(threeFragment3,"ThreeFragment_3");
                break;
            case R.id.profile_btn:
                FourFragment_4 fourFragment4=new FourFragment_4();
                addFragment(fourFragment4,"FourFragment_4");
                break;
            case R.id.other_btn:
                break;
            default:
                break;
        }


    }

    private Fragment curShowFragment;

    private void addFragment(Fragment fragment, String Tag) {

        FragmentManager manager = getChildFragmentManager();
        FragmentTransaction transaction = manager.beginTransaction();

        //先用Tag去内存中查找有没有这样的Fragment
        Fragment tempFragment = manager.findFragmentByTag(Tag);

        if (tempFragment == null) {
            if (curShowFragment != null) {
                transaction.hide(curShowFragment);
            }
            //第一个参数 fragment添加的容器的id   第三个参数，Fragment的标识  注意：同一个Fragment不能连续添加两次
            transaction.add(R.id.home_fragment_container, fragment, Tag);
            curShowFragment = fragment;
        } else {
            if (tempFragment != curShowFragment) {//说明要加添的Fragment当前没在显示
                transaction.hide(curShowFragment);
                transaction.show(tempFragment);
                curShowFragment = tempFragment;
            }
        }
        transaction.commit();
    }
}